﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WPF.BytesIO.Serial.Models
{
    //
    // 摘要:
    //     NotifyObject
    public class NotifyObject : INotifyPropertyChanged
    {
        private SynchronizationContext _SynchronizationContext = SynchronizationContext.Current;

        public event PropertyChangedEventHandler PropertyChanged;

        //
        // 参数:
        //   propertyName:
        protected void OnPropertyChanged(string propertyName)
        {
            try
            {
                if (this.PropertyChanged == null)
                {
                    return;
                }

                if (SynchronizationContext.Current == _SynchronizationContext || _SynchronizationContext == null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                    return;
                }

                _SynchronizationContext.Post(delegate (object t)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs((string)t));
                }, propertyName);
            }
            catch (Exception ex)
            {
                Console.WriteLine("PName : {0} - ", propertyName, ex.Message);
            }
        }
    }
}
